package org.boge.sharpdoc.controller;

import org.boge.sharpdoc.bean.DocGroup;
import org.boge.sharpdoc.bean.LayuiReturnData;
import org.boge.sharpdoc.bean.TreeData;
import org.boge.sharpdoc.service.DocGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("group")
public class GroupController {
    @Autowired
    private DocGroupService docGroupService;

    @PostMapping("getFirstStepGroup")
    @ResponseBody
    public LayuiReturnData getFirstStepGroup() {
        List<DocGroup> res = docGroupService.getAllFirstStepGroup();
        for (DocGroup tmp : res) {
            tmp.setChildren(null);
        }
        return new LayuiReturnData(0, true, "1", res.size(), res);
    }

    @PostMapping("getGroupChildren")
    @ResponseBody
    public LayuiReturnData getGroupChildren(Integer groupid) {
        List<DocGroup> res = docGroupService.getGroupChildren(groupid);
        for (DocGroup tmp : res) {
            tmp.setChildren(null);
        }
        return new LayuiReturnData(0, true, "数据查找成功！", res.size(), res);
    }

    @PostMapping("admin/getAllWithTreeDate")
    @ResponseBody
    public LayuiReturnData getAllWithTreeDate() {
        TreeData treeData = docGroupService.getTreeData();
        LayuiReturnData data = new LayuiReturnData(0, true, "数据查询成功！", 0);
        data.setData(new ArrayList<TreeData>());
        data.getData().add(treeData);
        return data;
    }

    @GetMapping("admin/setGroupPage")
    public String setGroupPage() {
        return "admin/setGroup";
    }

    @PostMapping("admin/editGroup")
    @ResponseBody
    public Boolean editGroup(DocGroup docGroup) {
        return docGroupService.changeName(docGroup);
    }

    @PostMapping("admin/addGroup")
    @ResponseBody
    public Integer addGroup(DocGroup newGroup) {
        return docGroupService.addNewGroup(newGroup);
    }

    @PostMapping("admin/delGroup")
    @ResponseBody
    public Boolean delGroup(DocGroup docGroup) {
        try {
            return docGroupService.deleteItem(docGroup);
        } catch (Exception exception) {
            return false;
        }
    }
}